package com.sinodata.bsm.cicp.finder.weblogic9;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.Query;
import javax.management.QueryExp;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.JmxUtil;

public class WorkManagerFinder extends Finder {

    private static final Logger logger = Logger.getLogger(WorkManagerFinder.class);

    /*@Override
    public List find(ResValue resValue, ResAttributeValValue[] attrVals, ResAttributeValue[] attributes) {
        List resources = new ArrayList();
        String ip = host;
        //MBeanServerConnection connection = JmxUtil.getConnection(JmxUtil.WEBLOGIC, "192.168.0.211", "7001", "weblogic","weblogic1");
        String port = attrVals[1].getAttrValue();
        ObjectName service;
        String name = null;
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            connector = JmxUtil.getConnection(JmxUtil.WEBLOGIC, ip + ":" + port);
            connection = connector.getMBeanServerConnection();
            service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
            Object domainConfiguration = connection.getAttribute(service, "DomainConfiguration");
            ObjectName[] appDeployments = (ObjectName[]) connection.getAttribute((ObjectName) domainConfiguration, "AppDeployments");
            QueryExp typefilter = Query.match(Query.attr("Type"), Query.value("ApplicationRuntime"));
            QueryExp appfilter = null;
            if ((appDeployments != null) && (appDeployments.length > 0)) {
                for (ObjectName objectName : appDeployments) {
                    name = objectName.getKeyProperty("Name");
                    QueryExp tfilter = Query.match(Query.attr("ApplicationName"), Query.value(name));
                    if (appfilter == null) {
                        appfilter = tfilter;
                    } else {
                        appfilter = Query.or(appfilter, tfilter);
                    }
                }
                QueryExp destfilter = Query.and(typefilter, appfilter);
                Set test = connection.queryNames(new ObjectName("com.bea:*"), destfilter);
                Iterator it = test.iterator();
                while (it.hasNext()) {
                    ObjectName objName = (ObjectName) it.next();
                    String applicationName = objName.getKeyProperty("Name");
                    String serverName = objName.getKeyProperty("ServerRuntime");
                    String attributeNames[] = new String[] { "Name" };
                    ObjectName[] workManagerRuntimes = (ObjectName[]) connection.getAttribute(objName, "WorkManagerRuntimes");
                    for (ObjectName objectName2 : workManagerRuntimes) {
                        Object attrValues[] = null;
                        attrValues = JmxUtil.getAttribute(connection, objectName2, attributeNames);
                        List oneResource = CreateOneResource(serverName, applicationName, attrValues[0].toString(), attributes);
                        resources.add(oneResource);
                    }
                }
            }

        } catch (Exception e) {
            logger.error("failed to find weblogic9 WorkManager" + e);
            resources = null;
        } finally {
            JmxUtil.closeConnector(connector);
        }
        return resources;
    }*/

    /*private List CreateOneResource(String serverName, String applicationName, String name, ResAttributeValue[] attributes) {
        List aRes = new ArrayList();
        ResValue res = new ResValue();
        ResAttributeValValue valValue = null;
        res.setIp(host); // host IP address
        res.setInternalId(host + "weblogic9" + serverName + applicationName + name); // Internal ID for agent
        res.setName(serverName + "." + applicationName + "." + name);
        aRes.add(res);
        valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[0].getId(), serverName);
        aRes.add(valValue);
        valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[1].getId(), applicationName);
        aRes.add(valValue);
        valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[2].getId(), name);
        aRes.add(valValue);
        return aRes;
    }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String port = (String) params.get("PORT");
        ObjectName service;
        String name = null;
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
            Object domainConfiguration = connection.getAttribute(service, "DomainConfiguration");
            ObjectName[] appDeployments = (ObjectName[]) connection.getAttribute((ObjectName) domainConfiguration, "AppDeployments");
            QueryExp typefilter = Query.match(Query.attr("Type"), Query.value("ApplicationRuntime"));
            QueryExp appfilter = null;
            if ((appDeployments != null) && (appDeployments.length > 0)) {
                for (ObjectName objectName : appDeployments) {
                    name = objectName.getKeyProperty("Name");
                    QueryExp tfilter = Query.match(Query.attr("ApplicationName"), Query.value(name));
                    if (appfilter == null) {
                        appfilter = tfilter;
                    } else {
                        appfilter = Query.or(appfilter, tfilter);
                    }
                }
                QueryExp destfilter = Query.and(typefilter, appfilter);
                Set test = connection.queryNames(new ObjectName("com.bea:*"), destfilter);
                Iterator it = test.iterator();
                while (it.hasNext()) {
                    ObjectName objName = (ObjectName) it.next();
                    String applicationName = objName.getKeyProperty("Name");
                    String serverName = objName.getKeyProperty("ServerRuntime");
                    String attributeNames[] = new String[] { "Name" };
                    ObjectName[] workManagerRuntimes = (ObjectName[]) connection.getAttribute(objName, "WorkManagerRuntimes");
                    for (ObjectName objectName2 : workManagerRuntimes) {
                        Object attrValues[] = null;
                        attrValues = JmxUtil.getAttribute(connection, objectName2, attributeNames);
                        String[] info = new String[6];
                        list.add(info);
                        info[0] = ip + "weblogic9" + serverName + applicationName + attrValues[0].toString();
                        info[1] = serverName + "." + applicationName + "." + attrValues[0].toString();
                        info[2] = serverName + " " + applicationName + "." + attrValues[0].toString();
                        info[3] = serverName; //服务器名字
                        info[4] = applicationName; //应用名字
                        info[5] = attrValues[0].toString(); //管理器名字
                    }
                }
            }
            return list;
        } catch (Exception e) {
            logger.error("failed to find weblogic9 WorkManager" + e);
            return null;
        } finally {
            JmxUtil.closeConnector(connector);
        }
    }
}
